<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" :key="updateKey"
        @search="search" @isUnfold="(v) => {
        unfoldFlag = v;
      }
        ">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <TableData :tableThead="tableThead" :dataList="tableData" :loading="loading" :showSummary="true">
      </TableData>
    </template>
    <template v-slot:pagination>
      <!-- <Pagination @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination> -->
      <el-pagination @size-change="(e) => { handleSizeChange(e), customSize = e }" @current-change="handleCurrentChange"
        :current-page="page" :page-sizes="[10, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, customSize]"
        :page-size="pageSize" layout="total,slot, sizes, prev, pager, next, jumper" background :total="total">
        <template #default>
          <el-input type="number" style="width: 150px;" v-model="customSize" placeholder="请输入内容" @change="() => {
        customSize = customSize < 1 ? customSize = 1 : Math.floor(customSize)
        handleSizeChange(customSize);
      }"><template slot="append">条/页</template></el-input>
        </template>
      </el-pagination>
    </template>
    <template v-slot:dialogModule>
      <waterAddEdit v-model="addEditShow" :stordocList="stordocList" @close="closeDia" @reflash="getTableList" />
      <waterImport v-model="importShow" @close="closeDia" @reflash="getTableList" />
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from "@/components-production-management/Pagination";
import BtnSlot from "@/components-production-management/BtnSlot";
import waterAddEdit from "./components/waterAddEdit";
import waterImport from "./components/waterImport";
import ncBasicInfoApi from "@/api/nc-basic-info/index";
import ncWaterQualityApi from "@/api/module/ncWaterQuality";
import reportApi from "@/api/module/report";
import TableData from "@/components-production-management/TableData";
import SearchForm from "@/components-production-management/SearchForm";
import { mymixin } from "@/utils/mixin";
export default {
  mixins: [mymixin],
  components: {
    MainSlot,
    Pagination,
    BtnSlot,
    waterAddEdit,
    TableData,
    SearchForm,
    waterImport,
  },
  data() {
    return {
      formItemList: [
        {
          label: "塘号",
          type: "input",
          param: "cdptname",
          isShow: true,
        },
        // {
        //   label: "塘号",
        //   type: "select",
        //   selectOptions: [],
        //   param: "ncWarehouseidLists",
        //   defaultSelect: "", // 下拉框默认选中项
        //   multiple: true,
        //   isShow: true,
        //   collapseTags: true,
        // },
        {
          label: "日期查询",
          type: "daterange",
          defaultSelect: [],
          param: "dateTime",
          isShow: true,
        },
      ],
      unfoldFlag: false,
      tableThead: [
        {
          isShow: true,
          prop: "dbilldate",
          label: "日期",
        },
        {
          isShow: true,
          prop: "cdptname",
          label: "塘号",
        },
        {
          isShow: true,
          prop: "cmaterialvname",
          label: "品种",
        },
        {
          isShow: true,
          prop: "nnum",
          label: "重量",
        },
        {
          isShow: true,
          prop: "vbdef1",
          label: "条数",
        },
      ],
      btnList: ["export"],
      addEditShow: false,
      tableData: [],
      loading: false,
      page: 1,
      total: 0,
      pageSize: 100,
      customSize: 100,
      stordocList: [],
      updateKey: 0,
      filterData: {},
      importShow: false,
    };
  },
  created() {
    this.queryStordoc();
    this.getTableList();
  },
  mounted() { },
  methods: {
    search(data) {
      this.filterData = JSON.parse(JSON.stringify(data));
      this.page = 1;
      this.getTableList();
    },
    getTableList() {
      const { page, pageSize, filterData } = this;
      const paylod = {
        page,
        size: pageSize,
      };
      const entity = JSON.parse(JSON.stringify(filterData));
      if (entity.dateTime && entity.dateTime.length > 0) {
        entity.startTime = filterData.dateTime[0] + " 00:00:00";
        entity.endTime = filterData.dateTime[1] + " 23:59:59";
        delete entity.dateTime;
      }
      if (filterData.ncWarehouseidLists) {
        let list = []
        list = filterData.ncWarehouseidLists.map(e => {
          return e.split('/')[1]
        })
        entity.ncWarehouseidLists = list.toString();
      }
      paylod.entity = entity;
      this.loading = true;
      reportApi
        .queryPageFinprodinReportByCondition(paylod)
        .then((res) => {
          if (res.success) {
            const { data } = res;
            this.tableData = data.content;
            this.total = data.totalElements;
            if (res.data.content.length === 0 && this.page !== 1) {
              this.page--;
              this.getTableList();
            }
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    queryStordoc() {
      ncBasicInfoApi.queryStordoc({}).then((res) => {
        this.stordocList = res.data;
        this.formItemList[0].selectOptions = this.stordocList.map((data) => {
          return {
            text: data.name,
            value: `${data.code}/${data.name}`,
          };
        });
        this.updateKey++;
      });
    },
    getBtnEvent(v) {
      this["btn" + this.capitalizeFirstLetter(v)]();
    },
    btnAdd() {
      this.addEditShow = true;
    },
    btnExport() {
      const { filterData } = this;
      const entity = JSON.parse(JSON.stringify(filterData));
      if (entity.dateTime && entity.dateTime.length > 0) {
        entity.startTime = filterData.dateTime[0] + " 00:00:00";
        entity.endTime = filterData.dateTime[1] + " 23:59:59";
        delete entity.dateTime;
      }
      if (filterData.ncWarehouseidLists) {
        let list = []
        list = filterData.ncWarehouseidLists.map(e => {
          return e.split('/')[1]
        })
        entity.ncWarehouseidLists = list.toString();
      }
      reportApi.finprodinReportReport({
        page: this.page,
        size: this.pageSize,
        entity: { ...entity }
      }).then((res) => {
        this.getFileNameAndDownload(res);
      });
    },
    btnImport() {
      this.importShow = true;
    },
    capitalizeFirstLetter(str) {
      if (!str) return "";

      // 将字符串的第一个字符转换为大写，剩余的字符保持不变
      return str.charAt(0).toUpperCase() + str.slice(1);
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getTableList();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getTableList();
    },
    closeDia() {
      this.addEditShow = false;
      this.importShow = false;
      this.getTableList();
    },
  },
};
</script>
<style lang="scss" scoped></style>
